using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;

namespace Clancy.Ui.ViewModels
{
    public abstract class ViewModelBase : ReactiveObject, IDisposable
    {
        protected readonly CompositeDisposable DisposableRxElements = new CompositeDisposable();

        public void Dispose()
        {
            DisposeMultipleElements(DisposableRxElements.ToList());
            DisposeElements();
        }

        protected static void DisposeMultipleElements(IList<IDisposable> disposables)
        {
            foreach (IDisposable dispo in disposables)
            {
                dispo.Dispose();
            }
            disposables.Clear();
        }

        protected abstract void DisposeElements();
    }
}